//package com.tiancheng.trade.merchant.exception.handler;
//
//import com.tiancheng.trade.commom.data.Result;
//import com.tiancheng.trade.commom.exception.AuthorizationException;
//import com.tiancheng.trade.merchant.exception.BusinessException;
//import com.tiancheng.trade.merchant.exception.error.CommonErrorEnum;
//import jakarta.validation.ConstraintViolationException;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.dao.DataIntegrityViolationException;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.validation.BindException;
//import org.springframework.web.bind.MethodArgumentNotValidException;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.bind.annotation.ExceptionHandler;
//import org.springframework.web.bind.annotation.ResponseBody;
//
///**
// * 全业务异常处理
// *
// * @author kellen
// * @date 2019-09-03
// * @changed kellen at 2021/07/15
// */
//@Slf4j
//@ControllerAdvice
//public class ControllerExceptionHandler {
//
//    @ExceptionHandler(Exception.class)
//    @ResponseBody
//    public ResponseEntity<Result<?>> handle(Exception exception) {
//        Result<?> res;
//
//        if (exception instanceof BusinessException) {
//            // 业务异常
//            BusinessException businessException = (BusinessException) exception;
//            log.error("业务异常: code={}, message={}", businessException.getCode(), businessException.getMessage());
//            res = Result.fail(businessException.getCode(), businessException.getMessage());
//        } else if (exception instanceof AuthorizationException) {
//            // 权限异常
//            AuthorizationException authorizationException = (AuthorizationException) exception;
//            log.error("权限异常: code={}, message={}", CommonErrorEnum.AUTH_ERROR.getCode(), authorizationException.getMessage());
//            res = Result.fail(CommonErrorEnum.AUTH_ERROR.getCode(), CommonErrorEnum.AUTH_ERROR.getMessage());
//        } else if (exception instanceof IllegalArgumentException) {
//            // 参数异常
//            IllegalArgumentException illegalArgumentException = (IllegalArgumentException) exception;
//            log.error("参数异常: code={}, message={}", CommonErrorEnum.ARGS_ERROR.getCode(), illegalArgumentException.getMessage());
//            res = Result.fail(CommonErrorEnum.ARGS_ERROR.getCode(), CommonErrorEnum.ARGS_ERROR.getMessage());
//        } else if (exception instanceof BindException || exception instanceof MethodArgumentNotValidException
//                || exception instanceof ConstraintViolationException) {
//            // 参数校验异常
//            log.error("参数校验异常: code={}, message={}", CommonErrorEnum.VALID_ERROR.getCode(), exception.getMessage());
//
//            if (exception instanceof BindException) {
//                StringBuilder sb = new StringBuilder();
//                ((BindException) exception).getBindingResult().getFieldErrors().forEach(d -> sb.append(d.getDefaultMessage())
//                        .append(";"));
//                res = Result.fail(CommonErrorEnum.VALID_ERROR.getCode(), CommonErrorEnum.VALID_ERROR.getMessage() + ": " + sb);
//            } else if (exception instanceof MethodArgumentNotValidException) {
//                StringBuilder sb = new StringBuilder();
//                ((MethodArgumentNotValidException) exception).getBindingResult().getFieldErrors().forEach(
//                        d -> sb.append(d.getDefaultMessage()).append(";"));
//                res = Result.fail(CommonErrorEnum.VALID_ERROR.getCode(), CommonErrorEnum.VALID_ERROR.getMessage() + ": " + sb);
//            } else {
//                StringBuilder sb = new StringBuilder();
//                ((ConstraintViolationException) exception).getConstraintViolations().forEach(
//                        d -> sb.append(d.getMessage()).append(";"));
//                res = Result.fail(CommonErrorEnum.VALID_ERROR.getCode(), CommonErrorEnum.VALID_ERROR.getMessage() + ": " + sb);
//            }
//        } else if (exception instanceof DataIntegrityViolationException) {
//            // SQL异常
//            DataIntegrityViolationException sqlException = (DataIntegrityViolationException) exception;
//            log.error("SQL异常: code={}, message={}", CommonErrorEnum.SQL_ERROR.getCode(), sqlException.getMessage());
//            res = Result.fail(CommonErrorEnum.SQL_ERROR.getCode(), CommonErrorEnum.SQL_ERROR.getMessage());
//        } else {
//            // 未定义的异常
//            log.error("未定义的异常: code={}, message={}", CommonErrorEnum.UNKNOWN.getCode(), exception.getMessage());
//            res = Result.fail(CommonErrorEnum.UNKNOWN.getCode(), CommonErrorEnum.UNKNOWN.getMessage());
//        }
//
//        return new ResponseEntity<>(res, HttpStatus.OK);
//    }
//}
